<?php

namespace System\library\database\sql;

/**
 * <b>Class</b> :        Sql<br>
 * <b>Fonction</b> :     Permet la création d'instance de connexion au server sql, et quelques reglages...
 * <br>
 * @version              1.0
 * @author               Steuf <dreadlokeur@gmail.com>
 * @license              Licence publique générale GNU http://www.gnu.org/licenses/gpl.html
 * @copyright            Copyright 2011 - MidichloriansPHP and contributors
 * @package              System
 * @subpackage           library\database\sql
 */
class Sql {

    private static $_debug = false;
    protected static $_explain = false;

    const TYPE_INT = 1;
    const TYPE_FLOAT = 2;
    const TYPE_STRING = 3;
    const TYPE_BLOB = 4;

    const FETCH_OBJECT = 1;
    const FETCH_ASSOC = 2;
    const FETCH_ARRAY = 3;
    const FETCH_ROW = 4;

    const PARAM_POSITIONAL = true;
    const PARAM_NAMED = false;

    private static $_database_class = null;

    public static function factory($configName, $databaseClass = null) {
        // if not defined get default
        if ($databaseClass === null)
            $databaseClass = self::$_database_class;

        if ($databaseClass === null)
            throw new \Exception('The database class name must be defined');

        // Check if is an object of MidichloriansPHP namespace
        if (class_exists('System\library\database\sql\driver\\' . $databaseClass))
            $class = '\System\library\database\sql\driver\\' . $databaseClass;
        else
            $class = $databaseClass;

        // Create new instance
        $inst = new \ReflectionClass($class);
        if (!in_array('System\library\database\sql\IDriver', $inst->getInterfaceNames()))
            throw new \Exception('Database class "' . $class . '" must be implement System\library\database\sql\IDriver');

        return $inst->newInstanceArgs(array($configName));
    }

    public static function setDatabaseClass($className) {
        if (!is_string($className) || empty($className))
            throw new \Exception('Class name must be an non empty string');
        self::$_database_class = $className;
    }

    public static function setDebug($debug, $explain = false) {
        if (!is_bool($debug))
            throw new \Exception('Debug parameter must be a boolean');
        if (!\is_bool($explain))
            throw new \Exception('Explain parameter must be a boolean');

        self::$_debug = $debug;
        self::$explain = $explain;
    }

    public static function isDebug() {
        return self::$_debug;
    }

    /**
     * Check if object is on explain mode
     * @return Boolean
     */
    public static function isExplain() {
        return self::$_explain;
    }

}
?>

